<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ page import="java.sql.*"%>
<%@ page import="java.io.*"%>
<%@ page import="net.sf.jasperreports.engine.*"%>
<%@ page import="net.sf.jasperreports.engine.util.*"%>
<%@ page import="net.sf.jasperreports.engine.JRException"%>
<%@ page import="net.sf.jasperreports.engine.JRExporterParameter"%>//excel
<%@ page import="net.sf.jasperreports.engine.export.JRXlsExporterParameter"%>//excel
<%@ page import="net.sf.jasperreports.engine.export.JRXlsExporter"%>//excel
<%


	Class.forName("org.gjt.mm.mysql.Driver").newInstance();
	String url = "jdbc:mysql://192.168.1.238:3306/monitor?useUnicode=true&characterEncoding=utf-8";
	//myDB为数据库名 
	Connection conn = DriverManager.getConnection(url, "monitor","admin");
	System.out.println(application.getRealPath("/myireport/report1.jasper")+ conn);
	File exe_rpt = new File(application.getRealPath("/myireport/report1.jasper"));
	JasperReport jr = (JasperReport) JRLoader.loadObjectFromLocation(exe_rpt.getPath());

	//rowid就是iReport的变量$P{rowid}的名称
	Map parameters = new HashMap();
	parameters.put("field", "field");

	try {
	System.out.println(exe_rpt.getPath()+"JasperFillManager=");
		// fill
		JasperPrint jasperPrint = JasperFillManager.fillReport(jr, parameters, conn);
		System.out.println(exe_rpt.getPath()+"jasperPrint="+jasperPrint);
		// excel输出
		ByteArrayOutputStream oStream = new ByteArrayOutputStream();
		JRXlsExporter exporter = new JRXlsExporter();

		exporter.setParameter(JRExporterParameter.JASPER_PRINT,jasperPrint);
		exporter.setParameter(JRExporterParameter.OUTPUT_STREAM,oStream);
		exporter.setParameter(JRXlsExporterParameter.IS_REMOVE_EMPTY_SPACE_BETWEEN_ROWS,Boolean.TRUE); // 删除记录最下面的空行
		exporter.setParameter(JRXlsExporterParameter.IS_ONE_PAGE_PER_SHEET,Boolean.FALSE);// 删除多余的ColumnHeader
		exporter.setParameter(JRXlsExporterParameter.IS_WHITE_PAGE_BACKGROUND,Boolean.FALSE);// 显示边框
		exporter.exportReport();

		byte[] bytes = oStream.toByteArray();

		if (bytes != null && bytes.length > 0) {
			response.reset();
			response.setContentType("application/vnd.ms-excel");
			response.setContentLength(bytes.length);
			ServletOutputStream ouputStream = response.getOutputStream();
			ouputStream.write(bytes, 0, bytes.length);
			ouputStream.flush();
			ouputStream.close();

		} else {
			out.print("bytes were null!");
		}

		conn.close();
	} catch (JRException ex) {
		out.print("Jasper Output Error:" + ex.getMessage());
	}
%>
